function setupTutorCallGrid() {
    $("#tutors").jqGrid({
        url : 'getTutorCallList.action',
        datatype : "json",
        colNames : [ '序号', '身份证号', '姓名', '性别', '调用名', '调用目的', '调用缘由', '备注' ],
        colModel : [ {
            name : 'no',
            index : 'no',
            width : 25,
            sortable: false
        }, {
            name : 'tutorIdNo',
            index : 'tutorIdNo',
            width : 120
        }, {
            name : 'tutorName',
            index : 'tutorName',
            width : 60
        }, {
            name : 'tutorSex',
            index : 'tutorSex',
            width : 30
        }, {
            name : 'callName',
            index : 'callName',
            width : 120
        }, {
            name : 'callAim',
            index : 'callAim',
            width : 60
        }, {
            name : 'callReason',
            index : 'callReason',
            width : 220
        }, {
            name : 'callRemark',
            index : 'callRemark',
            width : 250
        } ],
        rowNum : 10,
        height : 'auto',
        rowList : [ 10, 20, 30 ],
        pager : '#pager',
        sortname : 'id',
        viewrecords : true,
        sortorder : "desc",
        caption : "老师调用信息一览",
        multiselect : true,
        loadError : dealError
    });
    $("#tutors").jqGrid('navGrid', '#pager', {
        edit : false,
        add : false,
        del : false
    });
}

function setupTutorCallToolbar() {
    $("#add").button({
        icons : {
            primary : "ui-icon-plus"
        }
    }).click(function() {
        $("#id").val('');
        var params = {};
        $.ajax({
            url : "listTutor.action",
            type : "post",
            dataType : "json",
            data : params,
            success : setupTutorList,
            error : dealError
        });
    });

    $("#edit").button({
        icons : {
            primary : "ui-icon-pencil"
        }
    }).click(function() {
        var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要更新的老师调用信息！', 'alert', 'top');
            return false;
        }
        if (selects.length > 1) {
            message('请只选择一条需要更新的老师调用信息！', 'alert', 'top');
            return false;
        }
        var params = {
            selectIds : selects.toString()
        };
        $.ajax({
            url : "updateInitTutorCall.action",
            type : "post",
            dataType : "json",
            data : params,
            success : showTutorCall,
            error : dealError
        });
    });

    $("#delete").button({
        icons : {
            primary : "ui-icon-close"
        }
    }).click(function() {
        var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
        if (!selects || selects == '') {
            message('请选择需要删除的老师调用信息！', 'alert', 'top');
            return false;
        }
        confirm('是否确定删除？', 'center', doDeleteTutorCall);
    });

    $('#filter').button().click(function() {
        var sexCodeArray = getSexMatchCode($("#keyword").val());
        var callAimCodeArray = getCallAimMatchCode($("#keyword").val());
        $('#unfilter').button('option', 'disabled', false);
        var judgeSearch = {
            'tutorCallSearch.target.idNo' : $("#keyword").val(),
            'tutorCallSearch.target.name' : $("#keyword").val(),
            'tutorCallSearch.target.sex' : -1,
            'tutorCallSearch.sexCodes' : sexCodeArray.join(),
            'tutorCallSearch.callName' : $("#keyword").val(),
            'tutorCallSearch.callAim' : -1,
            'tutorCallSearch.callAimCodes' : callAimCodeArray.join(),
            'tutorCallSearch.callReason' : $("#keyword").val(),
            'tutorCallSearch.callRemark' : $("#keyword").val(),
            'tutorCallSearch.isOr' : true
        };
        var postData = $("#tutors").jqGrid("getGridParam", "postData");
        $.extend(postData, judgeSearch);
        $("#tutors").jqGrid("setGridParam", {
            search: true
        }).trigger("reloadGrid", [{page:1}]);
    });
    $('#unfilter').button({ disabled : true }).click(function() {
        $(this).button('option', 'disabled', true);
        var judgeSearch = {
            'tutorCallSearch.target.idNo' : '',
            'tutorCallSearch.target.name' : '',
            'tutorCallSearch.target.sex' : -1,
            'tutorCallSearch.callName' : '',
            'tutorCallSearch.callAim' : -1,
            'tutorCallSearch.callReason' : '',
            'tutorCallSearch.callRemark' : '',
            'tutorCallSearch.isOr' : true
        };
        var postData = $("#tutors").jqGrid("getGridParam", "postData");
        $.extend(postData, judgeSearch);
        $("#tutors").jqGrid("setGridParam", {
            search: true
        }).trigger("reloadGrid", [{page:1}]);
    });
}

function doDeleteTutorCall() {
    var selects = $('#tutors').jqGrid('getGridParam', 'selarrrow');
    var params = {
        selectIds : selects.toString()
    };
    $.ajax({
        url : "deleteTutorCall.action",
        type : "post",
        dataType : "json",
        data : params,
        beforeSend : validate,
        success : reload,
        error : dealError
    });
}

function validate(xhr) {
    ;
}

function setupTutorCallDialog() {
    $("#detail").dialog({
        autoOpen : false,
        show : "slide",
        height : 350,
        width : 550,
        modal : true,
        buttons : {
            登录 : function() {
                if ($('#name').val() == '') {
                    message('请输入调用名！', 'alert', 'top');
                    return false;
                }
                var params = $("#tutorCallForm").serialize();
                $.ajax({
                    url : "saveTutorCall.action",
                    type : "post",
                    dataType : "json",
                    data : params,
                    success : reload,
                    error : dealError
                });
                $("#detail").dialog("close");
                return false;
            },
            取消 : function() {
                $(this).dialog("close");
                return false;
            }
        },
        close : function() {
            $("input").val("").removeClass("ui-state-error");
            $("select").val("").removeClass("ui-state-error");
            $("textarea").val("").removeClass("ui-state-error");
        }
    });
}

function reload() {
    $("#tutors").trigger("reloadGrid");
}

function showTutorCall(result, status) {
    setupTutorList(result, status);
    var data = result.tutorCall;
    $("#detail").dialog("open");
    $("#targetId").val(data.target.id);
    $("#callAim").val(data.callAim);
    $("#name").val(data.callName);
    $("#reason").val(data.callReason);
    $("#remark").val(data.callRemark);
    $("#id").val(data.id);
}

function setupTutorList(data, status) {
    $("#targetId option").remove();
    var tutors = data.tutorList;
    for ( var i = 0; i < tutors.length; i++) {
        var id = tutors[i].id;
        var name = tutors[i].name + "(" + tutors[i].idNo + ")";
        var option = "<option value=" + id + ">" + name + "</option>";
        $("#targetId").append(option);
    }
    $("#detail").dialog("open");
}
